package gae.colopl.http.user;



import java.util.HashMap;
import java.util.Map;

public class SessionManagerLocal implements SessionManager {
    
    Map<String, SessionImpl> map = new HashMap<String, SessionImpl>();
    
    @Override
    public void put(String id, Session session) {
        SessionImpl impl = (SessionImpl)session;
        impl.release();
        map.put(id, impl);
    }

    @Override
    public Session get(String id) {
        SessionImpl session = (SessionImpl)map.get(id);
        if (session != null) {
            try {
                session.acquire();
            } catch (InterruptedException e) {
                e.printStackTrace();
                return null;
            }
        }
        return session;
    }

    @Override
    public void remove(String id) {
        SessionImpl session = map.remove(id);
        session.release();
    }

    @Override
    public Session createUser(String username, String password) {
        SessionImpl session = new SessionImpl(username, password, null, null, null, null);
        try {
            session.acquire();
        } catch (InterruptedException e) {
            e.printStackTrace();
            return null;
        }
        return session;
    }

}
